import openpyxl


def insert_text_files_to_spreadsheet(filenames):
    # 创建新的工作簿和工作表
    workbook = openpyxl.Workbook()
    sheet = workbook.active

    # 遍历文件列表
    for column, filename in enumerate(filenames, start=1):
        # 打开文本文件并读取内容
        with open(filename, 'r') as file:
            lines = file.readlines()

        # 将每行文本写入对应的列中
        for row, line in enumerate(lines, start=1):
            sheet.cell(row=row, column=column, value=line.strip())

    # 保存工作簿
    workbook.save('text_files.xlsx')
    workbook.close()

    print("内容已插入到电子表格中：text_files.xlsx")


if __name__ == '__main__':
    num_files = int(input("请输入要插入的文本文件数量："))
    filenames = []
    for i in range(num_files):
        filename = input(f"请输入第 {i + 1} 个文本文件的路径：")
        filenames.append(filename)

    insert_text_files_to_spreadsheet(filenames)
